// pages/exp/double/vote.js
var utils = require('../../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
      hideLoading: false,
      imgUrl: '', //'../../../images/loading.png', //
      curStep:-1,
      btnText:'下一张',
      items: [
          { value: '0', name: '非常难看' },
          { value: '1', name: '比较难看' },
          { value: '2', name: '不好看' },
          { value: '3', name: '没感觉' },
          { value: '4', name: '不难看' },
          { value: '5', name: '很美丽' },
          { value: '6', name: '非常美丽' }
      ]
  },

  curScore:-1,
  attendId:-1,
  imgId:-1,
  canSubmit:false,
  finish:false,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.showNavigationBarLoading()
      this.loadNextImg();
  },

  loadNextImg:function(){
      const that = this;
      utils.httpRequest({
          url: '/OrderedExp/double?r=' + Math.random(),
          success: function (res) {
              that.attendId = res.data.attendId;
              that.imgId = res.data.imgId;

              var url = 'https://cdn.water-crystal.org/double-blind-test/' + that.imgId + '.jpg';

              var rotation = parseInt(Math.random() * 4) * 90;
              if (rotation) {
                  url += ('-r' + rotation)
              }

              that.setData({
                  imgUrl: url,
                  curStep: res.data.step
              })

              if(parseInt(res.data.step)>=40){
                  that.setData({
                      btnText:'完成实验'
                  })
                  that.finish = true;
              }

              wx.setNavigationBarTitle({
                  title: res.data.step+'/'+40+' 双盲水结晶实验'
              })
              
          },
          fail: function () {
              console.log('get data failed')
              wx.showModal({
                  title: '网络错误',
                  content: '无法连接到服务器，请检查您的网络连接或稍后重试',
                  showCancel: false,
                  complete:function(){
                      wx.navigateBack({
                          delta: 1
                      })
                  }
              })
          }
      })
  },

  radioChange:function(e){
      this.curScore = e.detail.value
  },

  commitAndNext:function(){
      if(this.curScore==-1){
          wx.showModal({
              title: '请选择评分',
              content: '请点击图片下方的文字选择您对当前图片的评价，您必须完成对当前图片的评价后才能查看下一幅图片',
              showCancel: false
          })
      }else if(this.canSubmit){
          const that=this;
          this.submit2server(function(){
              if(that.finish){
                  wx.redirectTo({
                      url: 'finish?attendID='+that.attendId,
                  })
              }else{
                  wx.redirectTo({
                      url: 'vote',
                  })
              }
          });
      }else{
          wx.showModal({
              title: '操作失败',
              content: '请等待图片加载完毕或返回后重新进入实验',
              showCancel: false
          })
      }
  },

  submit2server:function(callback){
      wx.showLoading({
          title: '正在发送评分',
          mask: true
      })
      const that = this;
      utils.httpRequest({
          url: '/OrderedExp/commitScore',
          method: 'POST',
          data: {
              'attendID': that.attendId,
              'imgID': that.imgId,
              'score': that.curScore
          },
          header: {
              'content-type': 'application/x-www-form-urlencoded'
          },
          complete: function () {
              wx.hideLoading()
              wx.showToast({
                  title: '已完成',
                  icon: 'success',
                  duration: 1000
              });
              if (callback) callback();
          }
      })
  },

  firstLoad:false,

  imgLoaded: function () {
      this.canSubmit = true;
      this.setData({
          hideLoading:true
      })
      wx.hideNavigationBarLoading()
    //   if(this.firstLoad){
    //       wx.hideNavigationBarLoading()
    //       this.canSubmit = true;
    //   }else{
    //       this.firstLoad = true;
    //   }
  },

  showInstruction: function () {
      wx.showModal({
          title: '操作说明',
          content: '对不同水样的40张水结晶照片的美观程度进行盲评。照片经随机混淆，您不知道照片对应的水样是否经过意念处理。您可以随时关闭页面，参与结果将为您保存，可返回继续实验。实验结束后我们会发布实验结果。',
          showCancel: false
      })
  },


  onPullDownRefresh: function () { wx.stopPullDownRefresh() },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return { title: "意念影响水：双盲实验", path: '/pages/exp/exp', imageUrl: '../../../images/Love_and_Thanks.jpg' }
  }
})